let color = "{COOKIE_color}";
$(function () {
    $("#roomId").focus().on("keydown",function (e) {
        if (e.keyCode === 13){
            $("#join").click();
        }
    });
    $("#roomPassword").on("keydown",function (e) {
        if (e.keyCode === 13){
            $("#join").click();
        }
    });
    setInterval(function () {
        let element = $("#roomId");
        let value = element.val().toString();
        if (value.length > 0) {
            if (isNaN(value[value.length - 1])) {
                element.val(value.substr(0, value.length - 1));
            }
        }
    }, 0);
    $("#back").click(function () {
        window.open("./?/", "_self");
    });
    $("#join").click(function () {
        let id = $("#roomId").val().toString();
        let enablePwd = $("#enablePassword")[0].checked ? "yes" : "no";
        let roomPassword = "";
        if (enablePwd === "yes") {
            roomPassword = $("#roomPassword").val();
        }
        if (id.length === 4) {
            $.post("./?/createChat", {
                roomId: id,
                enablePassword: enablePwd,
                password: roomPassword
            }, function (text) {
                if (text === "success") {
                    dialog("提示","创建成功");
                    setTimeout(function () {
                        window.open("./?/main", "_self");
                    },800);
                } else if (text === "fail") {
                    dialog("提示","房间已存在");
                } else if (text === "error") {
                    dialog("提示","创建失败");
                } else {
                    alert(text);
                }
            });
        } else {
            dialog("提示","房间ID需要4位数字");
        }
    });
    if (!isMobile()){
        $("#join").hide();
    }else {
        $("#join-tip").hide();
    }
    $("#dialog-button").click(function () {
        $("#roomId").focus();
    });
    $("#enablePassword").click(function () {
        let $pd = $("#passwordDiv");
        if (this.checked){
            $pd.slideDown();
        }else {
            $pd.slideUp();
        }
    });
});
function dialog(title,message){
    $("#modal-dialog-title").text(title);
    $("#modal-dialog-message").text(message);
    $("#open-modal-dialog").click();
    setTimeout(function () {
        $("#dialog-button").focus();
    },300);
}